/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.xinxi.dms.service;

import com.xinxi.dms.dao.BaseDao;
import com.xinxi.dms.dao.CheckinDao;
import com.xinxi.dms.dao.RoomDao;
import com.xinxi.dms.dao.StudentDao;
import com.xinxi.dms.po.Room;
import com.xinxi.dms.po.Stay;
import com.xinxi.dms.po.Student;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author zhigu
 */
public class CheckinServiceIMPL implements ICheckinService{
    CheckinDao checkinDao = new CheckinDao();
         RoomDao roomDao = new RoomDao();
      StudentDao studentDao = new StudentDao();
    @Override
    public List<Stay> findAll_25(String unitno, String roomno) {
        String sql="select s.stuid,s.stuname,s.stusex,s.stuschool,s.state,"
                + "r.roomid,r.buildingno,r.unitno,r.roomno from student "
                + "as s,room as r where s.buildingno = r.buildingno and "
                + "s.unitno=r.unitno and s.roomno =r.roomno and"
                + " r.unitno = ? and r.roomno = ? and r.buildingno = 25";
        Object[] params={unitno,roomno};
        return checkinDao.query(sql, Stay.class, params);
    }

    @Override
    public int checkin(Integer stuid,Integer roomid) {
//        事务的四个特性： 1.原子性 2. 一致性 3. 隔离性 4. 持久性
//                原子性： 多个操作完成后，数据保持不变
//                一致性： 多个操作完成后，确保数据的一致
//                隔离性： 多个事务之间不互相影响
//                持久性： 多个事务操作完成后，数据会永久保留
        String sql_1 = "update student set state = '已入住' where stuid = ?";
        Object[] params_1={stuid};
        
        
        String sql_2 = "insert into stay(stuid,roomid,checkintime,checkinstate) "
                + "values(?,?,now(),?)";
        Object[] params_2={stuid,roomid,"入住"};
        
        
        String sql_3 = "select state,checknumber,remarks from room where roomid = ?";
        Object[] params_3={roomid};
        
        //修改房间的入住人数 和 房间状态
        String sql_4 = "update room set checknumber = checknumber+1 , state = ? where roomid = ?";
        Object[] params_4 = {"开放",roomid};
        
        String sql_5 = "update room set checknumber = checknumber+1 where roomid = ?";
        Object[] params_5 = {roomid};
        int result = 1;//1为成功，2为失败，3为房间已满
        Connection conn = new BaseDao().getConnection();
        try {
            //开启事务
            conn.setAutoCommit(false);
            //先执行查询房间状态
            Room room = roomDao.get(sql_3,Room.class, params_3);
            
            if(room.getState().equals("未开放")||room.getState()=="未开放")
                roomDao.update(conn, sql_4, params_4);//修改房间状态并入住人数+1
            else if(room.getChecknumber().equals(room.getRemarks()))
                result = 3;
            else
                roomDao.update(conn, sql_5, params_5);
            
            studentDao.update(conn, sql_1, params_1);
            roomDao.update(conn, sql_2, params_2);
            conn.commit();
        } catch (SQLException ex) {
            try {
                //事务回滚
                result =2;
                conn.rollback();
                //事务回滚
            } catch (SQLException ex1) {
                Logger.getLogger(CheckinServiceIMPL.class.getName()).log(Level.SEVERE, null, ex1);
            }
            Logger.getLogger(CheckinServiceIMPL.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    @Override
    public Student getState(int stuid) {
       String sql = "select state from student where stuid = ?";
       Object[] params = {stuid};
       return checkinDao.get(sql,Student.class, params);
    }

    @Override
    public List<Stay> findAll_23(String roomno) {
        String sql="select s.stuid,s.stuname,s.stusex,s.stuschool,s.state,"
                + "r.roomid,r.buildingno,r.unitno,r.roomno from student "
                + "as s,room as r where s.buildingno = r.buildingno and "
                + "s.unitno=r.unitno and s.roomno =r.roomno and"
                + " r.unitno = '一单元' and r.roomno = ? and r.buildingno = 23";
        Object[] params={roomno};
        return checkinDao.query(sql, Stay.class, params);
    }
    
}
